<?php
require_once 'Logger/Log.php';
class Application_Model_UserMapper
{

    protected $_dbTable;

    public function setDbTable($dbTable)
    {
        if (is_string($dbTable)) {
            $dbTable = new $dbTable();
        }
        if (!$dbTable instanceof Zend_Db_Table_Abstract) {
            throw new Exception('Invalid table data gateway provided');
        }
        $this->_dbTable = $dbTable;
        return $this;
    }

    public function getDbTable()
    {
        if (null === $this->_dbTable) {
            $this->setDbTable('Application_Model_DbTable_Users');
        }
        return $this->_dbTable;
    }

public function update(Application_Model_Users $users)
    {
    try{
        $data = array(
            'FIRST_NAME'=>$users->getFirstName(),
            'LAST_NAME'=>$users->getLastName(),
             'ADDRESS_LINE_1'=>$users->getAddress1(),
             'ADDRESS_LINE_2'=>$users->getAddress2(),
             'CITY'=>$users->getCity(),
             'STATE'=>$users->getState(),
             'COUNTRY'=>$users->getCountry(),
             'ZIP'=>$users->getZip(),
             'PHONE_NUMBER_1'=>$users->getPhone1(),
             'PHONE_NUMBER_2'=>$users->getPhone2(),
             'CELL_PHONE'=>$users->getCell(),
             'EMAIL_2'=>$users->getEmail2(),
             'ADMIN'=>$users->getAdmin(),
             'RESIDENT'=>$users->getResident(),
             'UPDATED_DATE'=>$users->getUpdatedDate(),
             'UPDATED_BY'=>$users->getUpdatedBy(),
             'ACTIVE_FLAG'=>$users->getActiveFlag()
        );


        $id = $users->getUserId();
        $this->getDbTable()->update($data, array('USER_ID = ?' => $id));
      
    }catch(Zend_Db_Statement_Exception $e){
        Zend_Registry::get('logger')->setEventItem('class', __CLASS__)->err($e->getMessage());
          throw new Exception("Technical difficulties experienced in updating User");
    }
    }

    public function updateProfile(Application_Model_Users $users)
    {
    try{
        $data = array(
            'FIRST_NAME'=>$users->getFirstName(),
            'LAST_NAME'=>$users->getLastName(),
             'ADDRESS_LINE_1'=>$users->getAddress1(),
             'ADDRESS_LINE_2'=>$users->getAddress2(),
             'CITY'=>$users->getCity(),
             'PHONE_NUMBER_1'=>$users->getPhone1(),
             'PHONE_NUMBER_2'=>$users->getPhone2(),
             'CELL_PHONE'=>$users->getCell(),
             'EMAIL_2'=>$users->getEmail2(),
             'UPDATED_DATE'=>$users->getUpdatedDate(),
             'UPDATED_BY'=>$users->getUpdatedBy()
        );


        $id = $users->getUserId();
        $this->getDbTable()->update($data, array('USER_ID = ?' => $id));

    }catch(Zend_Db_Statement_Exception $e){
        Zend_Registry::get('logger')->setEventItem('class', __CLASS__)->err($e->getMessage());
          throw new Exception("Technical difficulties experienced in updating Profile");
    }
    }

    public function insert(Application_Model_Users $users)
    {
    try{
        $data = array(
            'USER_ID'=>$users->getUserId(),
            'EMAIL_ADDRESS'=>$users->getEmailAddress(),
            'PASSWORD'=>MD5($users->getPassword()),
            'FIRST_NAME'=>$users->getFirstName(),
            'LAST_NAME'=>$users->getLastName(),
             'ADDRESS_LINE_1'=>$users->getAddress1(),
             'ADDRESS_LINE_2'=>$users->getAddress2(),
             'CITY'=>$users->getCity(),
             'STATE'=>$users->getState(),
             'COUNTRY'=>$users->getCountry(),
             'ZIP'=>$users->getZip(),
             'PHONE_NUMBER_1'=>$users->getPhone1(),
             'PHONE_NUMBER_2'=>$users->getPhone2(),
             'CELL_PHONE'=>$users->getCell(),
             'EMAIL_2'=>$users->getEmail2(),
             'ADMIN'=>$users->getAdmin(),
             'RESIDENT'=>$users->getResident(),
             'CREATED_DATE'=>$users->getCreatedDate(),
             'CREATED_BY'=>$users->getCreatedBy(),
             'UPDATED_DATE'=>$users->getUpdatedDate(),
             'UPDATED_BY'=>$users->getUpdatedBy(),
             'ACTIVE_FLAG'=>$users->getActiveFlag(),
            'CHANGE_PASSWORD'=>$users->getChangePassword()
        );

         $this->getDbTable()->insert($data);

    }catch(Zend_Db_Statement_Exception $e){
         Zend_Registry::get('logger')->setEventItem('class', __CLASS__)->err($e->getMessage());
          throw new Exception("Technical difficulties experienced in adding User");
    }
    }

    public function find(Application_Model_Users $users)
    {
         try{
            $select =  $this->getDbTable()->select();

            if(trim($users->getFirstName()) !=""){
                $select->where('FIRST_NAME like ?','%'. $users->getFirstName().'%');
            }

            if(trim($users->getLastName()) !=""){
                $select->where('LAST_NAME like  ?', '%'.$users->getLastName().'%');
            }

            if(trim($users->getAddress1()) !=""){
                $select->where('ADDRESS_LINE_1 like ?', '%'.$users->getAddress1().'%');
            }
             if(trim($users->getUserId()) !=""){
                $select->where('USER_ID = ?', $users->getUserId());
            }

            if(trim($users->getSortBy()) !=""){
                $select->order($users->getSortBy());
            }else{
                $select->order('FIRST_NAME ASC');
            }

            $rows =  $this->getDbTable()->fetchAll($select);

            return $rows;

         }catch(Zend_Db_Statement_Exception $e){
          Zend_Registry::get('logger')->setEventItem('class', __CLASS__)->err($e->getMessage());
          throw new Exception("Technical difficulties experienced while fectching  User");
     }
    }

    public function validateEmail(Application_Model_Users $users){
        try{
            $select =  $this->getDbTable()->select();
            $select->where('EMAIL_ADDRESS = ?', $users->getEmailAddress());

            $rows =  $this->getDbTable()->fetchAll($select);

            $count = $rows ->count();

            return $count;

        }catch(Zend_Db_Statement_Exception $e){
                Zend_Registry::get('logger')->setEventItem('class', __CLASS__)->err($e->getMessage());
                throw new Exception("Technical difficulties experienced while validating user");
        }

    }

    public function updatePassword(Application_Model_Users $users){
            try{
                $data = array(
                    'PASSWORD'=>MD5($users->getPassword()),
                    'CHANGE_PASSWORD'=>$users->getChangePassword(),
                    'UPDATED_DATE'=>$users->getUpdatedDate(),
                    'UPDATED_BY'=>$users->getUpdatedBy()
                     );

                 $this->getDbTable()->update($data, array('EMAIL_ADDRESS = ?' => $users->getEmailAddress()));
                 
        }catch(Zend_Db_Statement_Exception $e){
                Zend_Registry::get('logger')->setEventItem('class', __CLASS__)->err($e->getMessage());
                throw new Exception("Technical difficulties experienced while updating password");
        }
    }

      public function getPrimaryEmail() {
          try{
               $select =  $this->getDbTable()->select();
               $select->where("EMAIL_ADDRESS IS NOT NULL and  RESIDENT='Y'");
               $result = $this->getDbTable()->fetchAll($select);
               return $result;
          }catch(Zend_Db_Statement_Exception $e){
                Zend_Registry::get('logger')->setEventItem('class', __CLASS__)->err($e->getMessage());
                throw new Exception("Technical difficulties experienced while fetching primary email address");
        }

      }

      public function getSecondaryEmail() {
          try{
                $select =  $this->getDbTable()->select();
               $select->where("EMAIL_2 IS NOT NULL and  RESIDENT='Y'");
               $result = $this->getDbTable()->fetchAll($select);
               return $result;
          }catch(Zend_Db_Statement_Exception $e){
                Zend_Registry::get('logger')->setEventItem('class', __CLASS__)->err($e->getMessage());
                throw new Exception("Technical difficulties experienced while fetching secondary email address");
        }
      }
}
